package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.CommentUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.ContractUserDao;
import com.xbongbong.saas.domain.entity.ContractUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.subform.ContractTeamEnum;
import com.xbongbong.saas.enums.subform.PaymentSheetTeamEnum;
import com.xbongbong.saas.model.ContractUserModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/2/19 16:31
 * @since v1.0
 */
@Service("contractUserModel")
public class ContractUserModelImpl implements ContractUserModel {

    @Resource
    private ContractUserDao contractUserDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private EsHelper esHelper;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    /**
     * 插入
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer insert(ContractUserEntity entity) throws XbbException {
        Integer result = contractUserDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM, entity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
        paasEsModel.insert(insertDTO);
//        paasEsModel.insert(entity,IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM,entity.getCorpid(),entity.getId(),entity.getDataId());
        return result;
    }

    /**
     * 更新
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer update(ContractUserEntity entity) throws XbbException {
        Integer result = contractUserDao.update(entity);
        if (Objects.equals(result, 1)) {
            PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM, entity);
            InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            paasEsModel.update(saveDTO);
        }
//        paasEsModel.update(entity,IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM,entity.getCorpid(),entity.getId(),entity.getDataId());
        return result;
    }

    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return java.util.List<com.xbongbong.saas.domain.entity.CustomerUserEntity>
     * @author feng.zheng
     * @date 2019/2/16 14:22
     */
    @Override
    public List<ContractUserEntity> findEntitys(Map<String, Object> param) {
        return contractUserDao.findEntitys(param);

    }

    /**
     * 获取符合查询条件的记录总数
     *
     * @param param 查询条件
     * @return 符合查询条件的记录总数
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return contractUserDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 保存实体
     *
     * @param contractUserEntity 客户协同人团队实体
     * @return java.lang.Integer
     * @throws XbbException 数据库异常
     * @author feng.zheng
     * @date 2019/2/14 16:42
     */
    @Override
    public Integer save(ContractUserEntity contractUserEntity) throws XbbException {
        if (contractUserEntity.getId() == null || Objects.equals(0L, contractUserEntity.getId())) {
           return insert(contractUserEntity);
        }
        return update(contractUserEntity);
    }

    /**
     * 获取单个实体
     *
     * @param key    id主键
     * @param corpid 公司id
     * @return com.xbongbong.saas.domain.entity.ContractUserEntity
     * @author feng.zheng
     * @date 2019/2/14 16:43
     */
    @Override
    public ContractUserEntity getByKey(Long key, String corpid) {
        return contractUserDao.getByKey(key, corpid);
    }

    @Override
    public List<String> getContractUserIdList(String corpid, Long contractId, Integer isMain) {
        return contractUserDao.getContractUserIdList(corpid, contractId, isMain);
    }

    @Override
    public List<ContractUserEntity> getContractUserList(String corpid, Long contractId, Integer isMain) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put("dataId", contractId);
        param.put("isMain", isMain);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        return findEntitys(param);
    }

    @Override
    public List<String> getContractUserIdList(String corpid, List<String> contractIds, Integer isMain) {
        return contractUserDao.getContractUserIdListByIn(corpid, contractIds, isMain);
    }

    @Override
    public void disbandedTeamByCustomerId(String corpid, List<Long> customerIdList) throws XbbException{
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termsQuery(ContractEnum.getEsAttr4Keyword(ContractEnum.LINK_CUSTOMER),customerIdList));
        boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        List<PaasFormDataEntity> paasFormDataEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_CONTRACT,boolQueryBuilder,PaasFormDataEntity.class,Collections.singletonList(FieldTypeEnum.DATAID.getAlias()));
        List<Long> idList = new ArrayList<>();
        if (paasFormDataEntityList.isEmpty()){
            return;
        }
        for (PaasFormDataEntity entity : paasFormDataEntityList){
            idList.add(entity.getDataId());
        }
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("dataIdIn",idList);
        map.put("del",DelEnum.NORMAL.getDel());
        List<ContractUserEntity> contractUserEntityList = findEntitys(map);
        contractUserDao.deleteByContractIdIn(idList,corpid);
        for (ContractUserEntity entity : contractUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM,entity.getDataId());
        }
    }

    @Override
    public void delete(ContractUserEntity contractUser) throws XbbException{
        contractUser.setDel(1);
        contractUser.setUpdateTime((long) DateUtil.getInt());
        contractUserDao.update(contractUser);
        paasEsModel.delete(contractUser.getId(), contractUser.getCorpid(), IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM,contractUser.getDataId());
    }

    /**
     * 查询协同团队基础列表
     *
     * @param map 参数
     * @return 团队
     */
    @Override
    public List<UserTeamEntity> findBaseEntitys(Map<String, Object> map) {
        return contractUserDao.findBaseEntitys(map);
    }

    @Override
    public Integer deleteByContractIdIn(List<Long> contractIdIn, String corpid) throws XbbException{
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("contractIdIn",contractIdIn);
        List<ContractUserEntity> contractUserEntityList = findEntitys(map);
        Integer result = contractUserDao.deleteByContractIdIn(contractIdIn, corpid);
        for (ContractUserEntity entity : contractUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM,entity.getDataId());
        }
        return result;
    }

    @Override
    public List<Long> getDataIdList(String corpid, Integer isMain, List<String> userId) {
        return contractUserDao.getDataIdList(corpid,isMain,userId);
    }

    @Override
    public void contractUserInsert(UserEntity userEntity, Long contractId, Integer isMain) throws XbbException{
        String userId = userEntity.getUserId();
        String corpid = userEntity.getCorpid();
        String userName = userEntity.getName() == null ? "" : userEntity.getName();
        String avatar = userEntity.getAvatar() == null ? "" : userEntity.getAvatar();

        Map<String,Object> param = new HashMap<String,Object>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        CommentUtil.addToMap(param, "corpid", corpid);
        CommentUtil.addToMap(param, "contractId", contractId);
        CommentUtil.addToMap(param, "userId", userId);
        CommentUtil.addToMap(param, "del", 0);
        CommentUtil.addToMap(param, "pageNum", 1);
        CommentUtil.addToMap(param, "start", 0);

        List<ContractUserEntity> contractUserList = findEntitys(param);
        long now = DateUtil.getInt();
        if(contractUserList == null || contractUserList.size() < 1){
            ContractUserEntity contractUserEntity = new ContractUserEntity();
            contractUserEntity.setCorpid(corpid);
            contractUserEntity.setUserId(userId);
            contractUserEntity.setUserName(userName);
            contractUserEntity.setUserAvatar(avatar);
            contractUserEntity.setDataId(contractId);
            contractUserEntity.setIsMain(isMain);
            contractUserEntity.setAddTime(now);
            contractUserEntity.setUpdateTime(now);
            insert(contractUserEntity);
        }else{
            ContractUserEntity contractUserEntity = contractUserList.get(0);
            if(!contractUserEntity.getIsMain().equals(1)){
                contractUserEntity.setCorpid(corpid);
                contractUserEntity.setUserId(userId);
                contractUserEntity.setUserName(userName);
                contractUserEntity.setUserAvatar(avatar);
                contractUserEntity.setDataId(contractId);
                contractUserEntity.setIsMain(isMain);
                contractUserEntity.setUpdateTime(now);
                contractUserEntity.setDel(0);
                update(contractUserEntity);
            }
        }
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del) throws XbbException{
        batchUpdateByUserIdIn(corpid, dataIdIn, teamEntityList, isMain, del, WriteRequest.RefreshPolicy.WAIT_UNTIL);

    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException{
        contractUserDao.batchUpdateByIdIn(corpid, dataIdIn, isMain, del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(ContractTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy);
    }


    @Override
    public void insertBatch(List<ContractUserEntity> insertBatchList, boolean isImport) throws XbbException {
        contractUserDao.insertBatch(insertBatchList);

        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM, insertBatchList);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM));
        }
        
        if (isImport) {
            paasEsModel.insertBatch(insertDTOList, WriteRequest.RefreshPolicy.NONE);
        } else {
            paasEsModel.insertBatch(insertDTOList);
        }
    }

    @Override
    public ContractUserEntity getByUserIdContractId(String corpid, String userId, Integer contractId) {
        return contractUserDao.getByUserIdContractId(corpid, userId, contractId);
    }

    @Override
    public void deleteBatchUser(List<ContractUserEntity> contractUserEntityList, String corpid, Integer del) throws XbbException {
        if (CollectionsUtil.isEmpty(contractUserEntityList)) {
            return;
        }
        List<Long> idList = new ArrayList<>();
        List<InsertDTO> insertDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put(StringConstant.DEL, del);
        for (ContractUserEntity entity : contractUserEntityList){
            idList.add(entity.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType());
            insertDTO.setSource(map);
            insertDTOList.add(insertDTO);
        }
        contractUserDao.deleteBatchByIdList(idList, corpid, del);
        paasEsModel.updateBatch(insertDTOList);
    }

    @Override
    public Integer deleteBatch(List<Long> list, String corpid) throws XbbException{
        return contractUserDao.deleteBatch(list, corpid);
    }

    @Override
    public void updateBatchCustomerId(List<ContractUserEntity> contractUserEntityList, String corpid) throws XbbException {
        contractUserDao.updateBatchCustomerId(contractUserEntityList,corpid);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (ContractUserEntity entity : contractUserEntityList){
            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(ContractTeamEnum.CUSTOMER_ID.getDataAttr(), entity.getCustomerId());
            map.put(StringConstant.JSON_DATA, object);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType());
            insertDTO.setSource(map);
            insertDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertDTOList);
    }

    @Override
    public Map<Long, String> batchDeleteById(String corpid, List<Long> removeIdList) throws XbbException{
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("idIn",removeIdList);
        List<ContractUserEntity> contractUserEntityList = findEntitys(map);
        contractUserDao.batchDeleteById(corpid,removeIdList);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        Map<Long,String> contractUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ContractUserEntity entity : contractUserEntityList){
            contractUserMap.put(entity.getDataId(),entity.getUserId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType());
            Map<String,Object> sourceMap = new HashMap<>();
            sourceMap.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
            sourceMap.put(StringConstant.DEL,DelEnum.DELETE.getDel());
            insertDTO.setSource(sourceMap);
            insertDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertDTOList);
        return contractUserMap;
    }

    @Override
    public Integer updateBatch(List<ContractUserEntity> list, String corpid) throws XbbException {
        Integer success = contractUserDao.updateBatch(list, corpid);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (ContractUserEntity entity : list){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(entity.getDel())) {
                map.put("del", entity.getDel());
            }
            map.put("updateTime", DateTimeUtil.getInt());
            JSONObject object = new JSONObject();
            if (Objects.nonNull(entity.getIsMain())) {
                object.put(ContractTeamEnum.IS_MAIN.getDataAttr(), entity.getIsMain());
            }
            if (Objects.nonNull(entity.getDataId())) {
                object.put(ContractTeamEnum.DATA_ID.getDataAttr(), entity.getDataId());
            }
            if (Objects.nonNull(entity.getUserId())) {
                object.put(ContractTeamEnum.USER_ID.getDataAttr(), entity.getUserId());
            }
            if (Objects.nonNull(entity.getUserName())) {
                object.put(ContractTeamEnum.USER_NAME.getDataAttr(), entity.getUserName());
            }
            if (Objects.nonNull(entity.getUserAvatar())) {
                object.put(ContractTeamEnum.USER_AVATAR.getDataAttr(), entity.getUserAvatar());
            }
            if (Objects.nonNull(entity.getCustomerId())) {
                object.put(ContractTeamEnum.CUSTOMER_ID.getDataAttr(), entity.getCustomerId());
            }
            map.put(StringConstant.JSON_DATA, object);
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            insertDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertDTOList);
        return success;
    }


    /**
     *
     * @Description: 编辑团队成员标签
     * @param contractUserEntity
     * @return: void
     * @Author: shen.yang
     * @Date: 2022/1/17  14:15
    */
    @Override
    public void editLabel(ContractUserEntity contractUserEntity) throws XbbException {
        updateFormTeam(contractUserEntity);
    }

    /**
     * 更新协同人可编辑权限
     *
     * @param contractUserEntity
     */
    @Override
    public void updateEditFormPer(ContractUserEntity contractUserEntity) throws XbbException {
        updateFormTeam(contractUserEntity);
    }


    private void updateFormTeam(ContractUserEntity contractUserEntity) throws XbbException {
        Long updateTime = DateTimeUtil.getInt();
        contractUserEntity.setUpdateTime(updateTime);
        contractUserDao.update(contractUserEntity);
        InsertDTO insertDTO = new InsertDTO();
        String corpid = contractUserEntity.getCorpid();
        insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType() + "_" + corpid + "_" + contractUserEntity.getId());
        insertDTO.setParent(corpid + "_" + contractUserEntity.getDataId());
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("updateTime", updateTime);
        insertDTO.setSource(map);
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
        paasEsModel.update(insertDTO);
    }

    /**
     * 批量更新团队的人员为协同人身份以及协同人的可编辑权限
     *
     * @param corpid
     * @param dataIdIn
     * @param teamEntityList
     * @param isMain
     * @param needThrow
     * @throws XbbException
     */
    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow) throws XbbException {
        batchUpdateCoAndPerByUserIdIn(corpid, idIn, teamEntityList, isMain, needThrow, WriteRequest.RefreshPolicy.WAIT_UNTIL);

    }

    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        Integer formPermission = teamEntityList.get(BasicConstant.ZERO).getFormPermission();
        contractUserDao.batchUpdateCoAndPerByUserIdIn(corpid, idIn, isMain, formPermission);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(PaymentSheetTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy, needThrow);
    }


}
